package org.kou.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bao.bean.PhoneInfo;
import org.bao.service.impl.PhoneServiceImpl;
import org.bao.utils.MyUtils;

import com.alibaba.fastjson.JSON;

/**
 * Servlet implementation class QueryCarDemo 对购物车页面的增加操作
 */
@WebServlet("/QueryCarDemo")
public class QueryCarDemo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取前台数据对购物车进行增加操作
		// 前台页面编码
		response.setContentType("text/html;charset=UTF-8");
		// 获取传入的数量数据
		String phoneid = "26";
		// 获取传入的值
//		String phoneid = request.getParameter("phoneid");
		System.out.println("phoneid:" + phoneid);
		// 创建对象
		PhoneInfo phonek = MyUtils.getInstance(PhoneInfo.class);
		// 设置id值
		phonek.setPhoneid(phoneid);
		// 查询数据库，返回
		List<PhoneInfo> lstPhones = MyUtils.getInstance(PhoneServiceImpl.class).queryPhoneInfo(phonek);
		System.out.println("查询结果：" + lstPhones);
		// 借助第三方的工具将集合转换为json字符串【alibaba的fastjson插件包】
		// 将查询结果集list转换为json字符串
		String strJson = JSON.toJSONString(lstPhones);
		System.out.println("strJson:" + strJson);
		// 将对象写入session
		// request.getSession().setAttribute("phonename",lstPhones.get(0).getPhonename());
		// System.out.println(request.getSession().getAttribute("phonename"));

		// request.getSession().setAttribute("getPhoneprice",lstPhones.get(0).getPhoneprice());
		// request.setAttribute("phoneprice", lstPhones.get(0).getPhoneprice());

		// 向前台传送数据
		PrintWriter pw = response.getWriter();
		pw.write(strJson);
		pw.flush();
		// 使用重定向
		// response.sendRedirect("../shopping/shoppingcar.jsp");
		// 转发
		// request.getRequestDispatcher("../shopping/shoppingcar.jsp").forward(request,
		// response);
	}

}
